import requests


# 定义目标URL
url2 = "http://localhost:9090/user/login"  # 替换为实际API地址
# 准备请求头
headers = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    # "Authorization": "Bearer YOUR_ACCESS_TOKEN"  # 替换为实际的认证令牌
}

payload = {
    "username": "zhangxu",
    "password": "wyn123456",
}
try:
    # 发送POST请求
    response = requests.post(
        url2,
        json=payload,  # 自动序列化为JSON并设置Content-Type
        headers=headers
    )
    # 检查HTTP状态码
    if response.status_code == 200:
        print("测试登录接口成功")
        print("请求成功！")
        print("响应内容：", response.json())  # 解析JSON响应
        token=response.json()["data"]

        print("登录数据返回成功")
        print(token)


        # tokenName=response.json()["data"]["tokenName"]
        # tokenValue=response.json()["data"]["tokenValue"]
    else:
        print(f"请求失败！状态码：{response.status_code}")
        print("错误信息：", response.text)

except requests.exceptions.RequestException as e:
    print("请求异常：", e)
except ValueError:  # JSON解析异常
    print("响应不是有效的JSON格式")


##todo
print(token)
url7 = "http://localhost:9090/order/search/goods/1"  # 替换为实际API地址
headers1 = {
    # "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url7,headers=headers1)
print(response.text)

# /searchMyGoodsByKeys/{keys}/{pageNum}


print(token)
url8 = "http://localhost:9090/order/searchMyGoodsByKeys/苹果/1"  # 替换为实际API地址
headers1 = {
    # "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url8,headers=headers1)
print(response.text)



# url9 = "http://localhost:9090/order"  # 替换为实际API地址
# headers1 = {
#     "Content-Type": "application/json",  # 声明JSON内容类型
#     "Authorization": token  # 替换为实际的认证令牌
# }
#
#
# payload2 = {
#
#         "title": "项目订单",
#         "price": "125.50",
#         "content": "这是一个符合长度",
#         "orderStatus": 1,
#         "type": "standard",
#         "picture": "/images/order_1001.jpg",
#         "ownName": "张明",
#         "cooperationName": "李华科技",
#         "createTime": "2025-07-29T10:30:00.000Z",
#         "updateTime": "2025-07-29T10:45:00.000Z",
#         "address": "北京市海淀区"
#
# }
# # 准备请求头
# response = requests.post(url9,headers=headers1, json=payload2, )
# print(response.text)

# payload2 = {
#
#         "title": "遥遥领先",
#         "price": "125.50",
#         "content": "这是一个符合长度",
#         "orderStatus": 1,
#         "type": "standard",
#         "picture": "/images/order_1001.jpg",
#         "ownName": "张明",
#         "cooperationName": "李华科技",
#         "createTime": "2025-07-29T10:30:00.000Z",
#         "updateTime": "2025-07-29T10:45:00.000Z",
#         "address": "北京市海淀区"
#
# }
# url9 = "http://localhost:9090/order/145"  # 替换为实际API地址
# headers1 = {
#     "Content-Type": "application/json",  # 声明JSON内容类型
#     "Authorization": token  # 替换为实际的认证令牌
# }
#
# requests.put(url=url9,headers=headers1,json=payload2);



url10 = "http://localhost:9090/order/145"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

requests.delete(url=url10,headers=headers1);


requests.delete(url=url10,headers=headers1)
url11 = "http://localhost:9090/order/needs/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url11,headers=headers1)
print(response.text)

url12 = "http://localhost:9090/order/searchNeedsByKeys/小香薯/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url12,headers=headers1)
print(response.text)



url13 = "http://localhost:9090/order/searchMyNeedsByKeys/咩咩咩/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url13,headers=headers1)
print(response.text)





url14 = "http://localhost:9090/knowledge/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url14,headers=headers1)
print(response.text)

# selectById
#

url15 = "http://localhost:9090/knowledge/selectById/22"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url15,headers=headers1)
print(response.text)

# /{keys}/{pageNum}
url16 = "http://localhost:9090/knowledge/西瓜种植/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url16,headers=headers1)
print(response.text)



url17= "http://localhost:9090/knowledge/selectByUsername"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url17,headers=headers1)
print(17)
print(response.text)


# url18= "http://localhost:9090/knowledge"  # 替换为实际API地址
# headers1 = {
#     "Content-Type": "application/json",  # 声明JSON内容类型
#     "Authorization": token  # 替换为实际的认证令牌
# }
#
#
#
# payload18 =  {
#
#         "title": "遥遥领先",
#         "content": "三折叠",
#         "picPath": "....yyy",
#        }
# response = requests.post(url=url18,headers=headers1,json=payload18)
# print(18)
# print(response.text)


url19= "http://localhost:9090/knowledge/34"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
payload19 =  {

        "title": "遥遥领先",
        "content": "六折叠",
        "picPath": "....yyy",
       }
response = requests.put(url=url19,headers=headers1,json=payload19)
print(19)
print(response.text)



url20= "http://localhost:9090/knowledge/36"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
response = requests.delete(url=url20,headers=headers1)
print(20)
print(response.text)


# selectByKnowledge/{id}

url21= "http://localhost:9090/knowledge/selectByKnowledge/22"  # 替换为实际API地址
headers1 = {
    # "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
response = requests.get(url=url21,headers=headers1)
print(21)
print(response.text)

# /addByKnowledge/{id}/{content}

url22= "http://localhost:9090/knowledge/addByKnowledge/100/遥遥领先"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.post(url=url22,headers=headers1)
print(22)
print(response.text)


url23= "http://localhost:9090/question/selectId/112"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url23,headers=headers1)
print(23)
print(response.text)

url24= "http://localhost:9090/question/add"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}


data24={
  "expertName": "张专家",
  "questioner": "李先生",
  "phone": "13800138000",
  "plantName": "水稻",
  "title": "水稻叶子发黄怎么办？",
  "question": "最近发现水稻叶子开始发黄，这是什么原因？需要怎么治疗？",
  "answer": "可能是缺氮肥，建议施用适量氮肥并保持水分。",
  "status": 1
}
response = requests.post(url=url24,headers=headers1,json=data24)
print(24)
print(response.text)



url25= "http://localhost:9090/question/findAllQues/1"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url25,headers=headers1)
print(25)
print(response.text)

url26= "http://localhost:9090/question/findPageQues/玉米/1"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url26,headers=headers1)
print(26)
print(response.text)


url27= "http://localhost:9090/question/findExpert/1"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url27,headers=headers1)
print(27)
print(response.text)

url28= "http://localhost:9090/question/findExpertByKeys/高歌/1"  # 替换为实际API地址
headers1 = {
     "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}

response = requests.get(url=url28,headers=headers1)
print(28)
print(response.text)